<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\index\controller;
use app\common\model\Address;
use app\common\model\Category;
use app\common\model\Goods;

/**
 * 前端首页控制器
 */
class Index extends IndexBase
{
    
    // 首页
    public function index($cid = 0)
    {

        $category = new Category();
        $where = [];
        $category_list = $category->search();
        $goodsLists['lun_goods'] = $this->getGoodsLists(1,4)['data'];
        $goodsLists['hot_goods'] = $this->getGoodsLists(2,6)['data'];
        $goodsLists['common_goods'] = $this->getGoodsLists(0,6)['data'];
        $this->assign('category_list',$category_list->toArray()['data']);
        $this->assign('goods_list',$goodsLists);
        return $this->fetch('index');
    }
    
    // 详情
    public function details($id = 0)
    {
        $uid = session('userid');
        if(!$uid){
            $this->jump(RESULT_REDIRECT, '未登录', url('user/login'));
        }

        $where = [];
        
        !empty((int)$id) && $where['id'] = $id;
        
        $goods = new Goods();

        $data = $goods->getGoodsById($where);

        $img = [
            'prod_1'=>[
                'main'=>[
                    'orig'=>get_picture_url($data['img_ids']),
                    'main'=>get_picture_url($data['img_ids']),
                    'thumb'=>get_picture_url($data['img_ids'])
                ],
                'gallery'=>[
                    'item_0'=>[
                        'orig'=>get_picture_url($data['img_ids']),
                        'main'=>get_picture_url($data['img_ids']),
                        'thumb'=>get_picture_url($data['img_ids'])
                    ]
                ],
            ],
        ];
        $imgs_arr = explode(',',$data['img_ids']);
        for($i=1;$i<count($imgs_arr);$i++){
            $img['prod_1']['gallery']['item'.($i-1)] = [
                'orig'=>get_picture_url($imgs_arr[$i]),
                'main'=>get_picture_url($imgs_arr[$i]),
                'thumb'=>get_picture_url($imgs_arr[$i])
            ];
        }
        $str = json_encode($img);
        $this->assign('goodsDetail', $data);
        $this->assign('str',$str);
        $category = new Category();
        $category_list = $category->search();
        $this->assign('category_list',$category_list->toArray()['data']);
        
        return $this->fetch('details');
    }

    public function getGoodsLists($show_position,$pageSize)
    {
        $goods = new Goods();
        $list = $goods->search(['show_position'=>$show_position],'*','id desc',0,$pageSize);
        return $list->toArray();
    }

    public function shopCar()
    {

        $uid =  userLogin();
        if(!$uid){
            $this->jump(RESULT_REDIRECT, '未登录', url('index/user/login'));
        }
        $goods  =  (new \app\common\model\Car())->search(['userid'=>$uid],$field = true, $order = '',100)->toArray();
        foreach ($goods['data'] as &$value){
            $value['goodsInfo'] = (new Goods())->getGoodsById(['id'=>$value['goodsid']]);
        }
        $address_list = (new Address())->search(['userid'=>$uid]);
        if(empty($address_list->toArray()['data'])){
            $this->jump(RESULT_REDIRECT, '未登录', url('index/user/address'));
        }
        $category_list = (new Category())->search();
        $this->assign('category_list',$category_list->toArray()['data']);
        $this->assign('address_list',$address_list->toArray()['data']);
        $this->assign('goodsList',$goods['data']);
        return $this->fetch('shopCar');
    }

    public function category($cate=0)
    {
        if($cate){
            $where['category'] = $cate;
        }
        $category = new Category();
        $category_list = $category->search();
        $list = (new Goods())->search($where);
        $this->assign('category_list',$category_list->toArray()['data']);

        $this->assign('list',$list);
        return $this->fetch('category');
    }

    public function search($search=0)
    {
        if($search){
            $where['name|description'] =['like', "%$search%"];
        }
        $where['status'] = 1;
        $category = new Category();
        $category_list = $category->search();
        $list = (new Goods())->search($where);
        $this->assign('category_list',$category_list->toArray()['data']);

        $this->assign('list',$list);
        return $this->fetch('category');
    }
}
